﻿using CMS.Models;
using CMS.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class CargoTypeViewModel:ViewModelBase
    {
        private List<CargoType> cargoTypes=new List<CargoType> ();
        /// <summary>
        /// 所有物资类型
        /// </summary>
        public List<CargoType> CargoTypes
        {
            get
            {
            return cargoTypes;                 
            }
            set
            {
                cargoTypes = value;
                RaisePropertyChanged (nameof(CargoTypes));
            }
        }
        public CargoTypeViewModel()
        {
            cargoTypes = new CargoTypeProvider().Select();
        }
        /// <summary>
        /// 打开添加新纪录对话框
        /// </summary>
        /// 
        public RelayCommand OpenAddCargoTypeWindowCommand
        {
            get 
            {
                return new RelayCommand(() =>
            {
                var window = new AddCargoTypeWindow();
                window.ShowDialog ();
                //刷新数据
                CargoTypes = new CargoTypeProvider().Select();
            });
            }
        }
        /// <summary>
        /// 删除物资类型命令
        /// </summary>
        public RelayCommand<object> DeleteCargoTypeCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is CargoType model)) return;
                    var cardgo = new CargoProvider().Select().FindAll(t => t.Typeld == model.Id);
                    if (cardgo != null && cardgo.Count>0){MessageBox.Show("当前类型已经有用");return; }
                    var count = new CargoTypeProvider().Delete(model);
                    if (count > 0) MessageBox.Show("操作成功","温馨提示");
                    //删除数据
                    CargoTypes = new CargoTypeProvider().Select();
                });
            }
        }
      
    }
}
